package presentation.playgameForm_UI;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JWindow;
import javax.swing.Timer;

import business.playgameForm_Evt.GameMode_Evt;

import presentation.menuForm_UI.MenuForm_UI;
import presentation.optionForm_UI.OptionForm_UI;

@SuppressWarnings("serial")
public class MenuOption_UI extends JWindow {

	private JButton			btResume	= new JButton();
	private JButton			btSave		= new JButton();
	private JButton			btOption	= new JButton();
	private JButton			btBack		= new JButton();

	private JPanel			pnMain		= new JPanel();
	private Timer			timer;
	public JFrame			frame;
	private JPanel			panelButton;
	private OptionForm_UI	optionForm;
	
	private GameMode_Evt event = new GameMode_Evt();
	
	private int					gameMode_point		= 0;
	private int					gameMode_score		= 0;
	private int					gameMode_target		= 50;
	private int					gameMode_time		= 120;
	private int					gameMode_level		= 0;
	private String				gameMode_textTemp	= "";
	
	private int gameMode;

	public MenuOption_UI(JFrame frame, Timer timer, JPanel panelButton, int gameMode) {
		super(frame);
		this.frame = frame;
		this.timer = timer;
		this.panelButton = panelButton;
		this.gameMode = gameMode;
		
		optionForm = new OptionForm_UI(frame);
		setButtonProperties();
		setLayoutPanel();
		setMainLayout();
		buttonEvent();
		keyEvent();

		this.setLocationRelativeTo(frame);
	}

	private void setButtonProperties() {
		btResume.setFont(new Font("Calibri", 0, 16));
		btSave.setFont(new Font("Calibri", 0, 16));
		btOption.setFont(new Font("Calibri", 0, 16));
		btBack.setFont(new Font("Calibri", 0, 16));

		btResume.setText("Resume game");
		btSave.setText("Save game");
		btOption.setText("Option");
		btBack.setText("Back to main menu");
	}

	private void setLayoutPanel() {
		pnMain.setLayout(null);
		pnMain.setBorder(BorderFactory.createTitledBorder(""));
		pnMain.setBackground(new Color(201, 228, 255));
		pnMain.add(btResume);
		pnMain.add(btSave);
		pnMain.add(btOption);
		pnMain.add(btBack);

		btResume.setBounds(10, 10, 200, 60);
		btSave.setBounds(btResume.getX(), btResume.getY() + btResume.getHeight() + 10, btResume.getWidth(), btResume.getHeight());
		btOption.setBounds(btResume.getX(), btSave.getY() + btSave.getHeight() + 10, btResume.getWidth(), btResume.getHeight());
		btBack.setBounds(btResume.getX(), btOption.getY() + btOption.getHeight() + 10, btResume.getWidth(), btResume.getHeight());
		
		if(gameMode == GameMode_UI.PRACTICE_MODE)
		{
			btSave.setEnabled(false);
			btSave.setRequestFocusEnabled(false);
		}
	}

	private void setMainLayout() {
		this.setLayout(null);
		this.add(pnMain);
		pnMain.setBounds(1, 1, 220, 290);
		this.setOpacity(0.9f);

		this.setSize(222, 292);
	}

	private void buttonEvent() {
		btBack.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent evt) {
				frame.setVisible(false);
				MenuForm_UI.frame.setVisible(true);
				escapeEvent();
				timer.stop();
			}
		});
		btResume.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent evt) {
				escapeEvent();
			}
		});
		btOption.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent evt) {
				optionForm.setVisible(true);
			}
		});
		btSave.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent evt) {
				saveGame();
				JOptionPane.showMessageDialog(null, "Your current game has been saved!", "Message", JOptionPane.INFORMATION_MESSAGE);
				escapeEvent();
			}
		});

	}

	private void keyEvent() {
		btBack.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent evt) {
				if (evt.getKeyCode() == KeyEvent.VK_UP)
					btOption.requestFocus();
				if (evt.getKeyCode() == KeyEvent.VK_DOWN)
					btResume.requestFocus();
				if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
					escapeEvent();
				}
			}
		});
		btResume.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent evt) {
				if (evt.getKeyCode() == KeyEvent.VK_UP)
					btBack.requestFocus();
				if (evt.getKeyCode() == KeyEvent.VK_DOWN)
				{
					if(gameMode != GameMode_UI.PRACTICE_MODE)
						btSave.requestFocus();
					else
						btOption.requestFocus();
				}
				if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
					escapeEvent();
				}
			}
		});
		
		btSave.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent evt) {
				if (evt.getKeyCode() == KeyEvent.VK_UP)
					btResume.requestFocus();
				if (evt.getKeyCode() == KeyEvent.VK_DOWN)
					btOption.requestFocus();
				if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
					escapeEvent();
				}
			}
		});
		btOption.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent evt) {
				if (evt.getKeyCode() == KeyEvent.VK_UP)
				{
					if(gameMode != GameMode_UI.PRACTICE_MODE)
						btSave.requestFocus();
					else
						btResume.requestFocus();
				}
				if (evt.getKeyCode() == KeyEvent.VK_DOWN)
					btBack.requestFocus();
				if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
					escapeEvent();
				}
			}
		});
	}

	private void escapeEvent() {
		timer.start();
		panelButton.setVisible(true);
		setVisible(false);
	}
	
	public void setValue(int gameMode_level, int gameMode_score, int gameMode_target, int gameMode_point, int gameMode_time, String gameMode_textTemp)
	{
		this.gameMode_level = gameMode_level;
		this.gameMode_score = gameMode_score;
		this.gameMode_target = gameMode_target;
		this.gameMode_point = gameMode_point;
		this.gameMode_time = gameMode_time;
		this.gameMode_textTemp = gameMode_textTemp;
	}
	
	private void saveGame()
	{
		event.saveGame(this.gameMode_level, this.gameMode_score, this.gameMode_target, this.gameMode_point, this.gameMode_time, this.gameMode_textTemp);
	}
	

}
